/**
 * 
 */
package com.scpii.universal.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.AppIconDao;
import com.scpii.universal.hibernate.entity.AppIconTab;
import com.scpii.universal.service.AppIconService;

/**
 * genericWeb
 * @author weny
 * 2012-7-8下午09:31:10
 */
@Service
public class AppIconServiceImpl extends BaseServiceImpl<AppIconTab, Integer>
		implements AppIconService {
	@Resource
	private AppIconDao appIconDao;

	@Resource
	public void setBaseDao(AppIconDao appIconDao) {
		super.setBaseDao(appIconDao);
	}


	@Override
	public void updateLoadingIcon(int loadingIconId, String loadingIconUrl) {
		//更新LOADING URL
		StringBuffer hql = new StringBuffer("update AppIconTab a set ");
		hql.append("a.datUpdateDatetime=:datUpdateDatetime");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("datUpdateDatetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//date
		hql.append(",a.varIconUrl=:varIconUrl");
		argsMap.put("varIconUrl", loadingIconUrl.replace(getServerUrl(), ""));//url
		hql.append(",a.intIconType=:intIconType");
		argsMap.put("intIconType", GenericConstants.ICON_TYPE_LOADING_NEED_TO_UPD);//更改iconType标识客户端需要更新LOADING
		hql.append(" where a.intIconId=:intIconId");
		argsMap.put("intIconId", loadingIconId);
		appIconDao.hqlOperation(hql.toString(), argsMap);
	}
	
	public void updateHideIcon(int iconId) {
		StringBuffer hql = new StringBuffer("update AppIconTab a set ");
		hql.append("a.datUpdateDatetime=:datUpdateDatetime");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("datUpdateDatetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));//date
		hql.append(",a.intDeleteSign=:intDeleteSign");
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_NOT_SHOW);
		hql.append(" where a.intIconId=:intIconId");
		argsMap.put("intIconId", iconId);
		appIconDao.hqlOperation(hql.toString(), argsMap);
	}


	@Override
	public List<AppIconTab> getMaterialIcons(int appId, int defineType) {
		String[] properties = {"intIconId","intIconName","varIconUrl"};
		StringBuffer hql = new StringBuffer(appIconDao.generateHqlByProperties(properties));
		Map<String, Object> argsMap = new HashMap<String, Object>();
		hql.append("where entity.intIconDefineType=:intIconDefineType");
		argsMap.put("intIconDefineType", defineType);
		hql.append(" and entity.intDeleteSign=:intDeleteSign");
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		if (defineType == GenericConstants.ICON_DEFINE_TYPE_USER) {
			//用户素材不包含 0：Loading图标、5：应用图标
			hql.append(" and entity.intIconType not in(");
			hql.append(GenericConstants.ICON_TYPE_LOADING + ",");
			hql.append(GenericConstants.ICON_TYPE_APP_ICON + ")");
			//隐藏的图标不显示(用户删除的图标)
		}
		if (defineType != GenericConstants.ICON_DEFINE_TYPE_SYS) {
			//系统素材不分应用
			hql.append(" and entity.intAppId=:intAppId");
			argsMap.put("intAppId", appId);
		}
		hql.append(" order by entity.intIconId desc");
		List<AppIconTab> lst = appIconDao.hqlGetList(hql.toString(), argsMap);
		if (lst != null && lst.size() > 0) {
			return lst;
		}
		return null;
	}


	@Override
	public String getIconURLById(int id) {
		if (id == 0)
			return "";
		return appIconDao.getIconUrlById(id);
	}

}
